namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class JobTitleTypeBLL
    {
        public int Add(UserJobTitleType model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserJobTitleType(");
            builder.Append("TypeName");
            builder.Append(") values (");
            builder.Append("@TypeName");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.TypeName;
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserJobTitleType ");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(TypeID) from UserJobTitleType");
            builder.Append(" where ");
            builder.Append(" TypeID = @TypeID  ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            return DBHelper.Exists(builder.ToString(), cmdParms);
        }

        public List<UserJobTitleType> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserJobTitleType ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            List<UserJobTitleType> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserJobTitleType>();
                UserJobTitleType item = null;
                foreach (DataRow row in table.Rows)
                {
                    UserJobTitleType type2 = new UserJobTitleType {
                        TypeID = int.Parse(row["TypeID"].ToString()),
                        TypeName = row["TypeName"].ToString()
                    };
                    item = type2;
                    list.Add(item);
                }
            }
            return list;
        }

        public UserJobTitleType GetModel(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select TypeID, TypeName  ");
            builder.Append("  from UserJobTitleType ");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            UserJobTitleType type = new UserJobTitleType();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    type.TypeID = int.Parse(set.Tables[0].Rows[0]["TypeID"].ToString());
                }
                type.TypeName = set.Tables[0].Rows[0]["TypeName"].ToString();
                return type;
            }
            return null;
        }

        public bool Update(UserJobTitleType model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserJobTitleType set ");
            builder.Append(" TypeName = @TypeName  ");
            builder.Append(" where TypeID=@TypeID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4), new SqlParameter("@TypeName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.TypeID;
            cmdParms[1].Value = model.TypeName;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

